﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using Oroboros;
using Oroboros.Objects;
using Oroboros.Save_Data;

namespace Save_Editor.Controls
{
    /// <summary>
    /// Interaction logic for ctrlItemSaveData.xaml
    /// </summary>
    public partial class ctrlItemSaveData : UserControl
    {
        public ctrlItemSaveData()
        {
            InitializeComponent();

            FieldInfo[] fInfos = typeof(ItemDB).GetFields();
            foreach (FieldInfo f in fInfos) bmInvent.Items.Add(new State((string[])f.GetValue(null)));

            fInfos = typeof(AmmoDB).GetFields();
            foreach (FieldInfo f in fInfos) ammobox.Items.Add(f.GetValue(null));
        }

        public ItemSaveData Item
        {
            get
            {
                try
                {
                    return (ItemSaveData)this.DataContext;
                }
                catch
                {
                    return null;
                }
            }
            set
            {
                this.DataContext = value;
            }
        }
    }

    public class NullIntToString : IValueConverter
    {
        /// <summary>
        /// Int to String
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (int)value;
        }


        /// <summary>
        /// String to Int
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() == typeof(string))
            {
                if (string.IsNullOrEmpty((string)value))
                {
                    if (targetType == typeof(int))
                        return 0;
                    else if (targetType == typeof(int?))
                        return null;
                    else
                        return 0;
                }
                else
                {
                    int r= 0;
                    if (!int.TryParse((string)value, out r))
                        r = 30;
                    return r;
                }
            }
            return (int)value;
        }
    }

    public class NullStringConv : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
            else
                return (string)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (string.IsNullOrEmpty((string)value))
            {
                return null;
            }
            else
                return (string)value;
        }
    }


    public class State
    {
        public string[] data;

        public string weapon { get { return data[0]; } }
        public string ammo { get { return data[1]; } }
        public State(string[] d)
        {
            data = d;
        }

        public override string ToString()
        {
            return data[0];
        }
    }

}
